﻿using System;
using System.Collections.Generic;

namespace WebBuddy.UriBuddy
{
    /// <summary>
    /// Represents a query string
    /// </summary>
    public class ParameterDictionary : Dictionary<String, String>
    {

        /// <summary>
        /// Create a new <see cref="ParameterDictionary"/>
        /// </summary>
        public ParameterDictionary() : base() { }

        /// <summary>
        /// Create a new <see cref="ParameterDictionary"/> object
        /// </summary>
        /// <param name="parameters">A list of parameters to use</param>
        public ParameterDictionary(IDictionary<String, String> parameters) : base(parameters) { }


        /// <summary>
        /// Create a new <see cref="Uri"/> with our Parameters
        /// </summary>
        /// <param name="baseUri">The base of this uri</param>
        /// <returns>A <see cref="Uri"/> that complete with the query string</returns>
        public Uri CreateUri(Uri baseUri)
        {
            UriBuilder ub = new UriBuilder(baseUri);
            ub.Query = this.ToQueryString();
            return ub.Uri;
        }

        /// <summary>
        /// Create a new <see cref="Uri"/> with our Parameters
        /// </summary>
        /// <param name="baseUri">The base of this uri</param>
        /// <param name="relativePath">The relative path</param>
        /// <exception cref="ArgumentNullException">Raised when the <paramref name="relativePath"/> is null</exception>
        /// <returns>A <see cref="Uri"/> that complete with the query string</returns>
        public Uri CreateUri(Uri baseUri, String relativePath)
        {
            if ( relativePath == null )
            {
                throw new ArgumentNullException("relativePath", "The relativePath cannot be null");
            }
            UriBuilder ub = new UriBuilder(baseUri);
            ub.Path = relativePath;
            ub.Query = this.ToQueryString();
            return ub.Uri;
        }

        /// <summary>
        /// Converts this object to a string
        /// </summary>
        /// <returns>The stringer-ized query string</returns>
        public override string ToString()
        {

            return this.ToQueryString();
        }

    }
}
